package com.example.firstprojectnew.controller;

import com.example.firstprojectnew.common.WrapMapper;
import com.example.firstprojectnew.common.Wrapper;
import com.example.firstprojectnew.entity.User;
import com.example.firstprojectnew.service.UserService;
import com.example.firstprojectnew.vo.LoginVO;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.List;


/**
 * @Author: lihua
 * @Date: 2025/3/19 09:07
 * @Version: v1.0.0
 * @Description:
 **/

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @PostMapping("/login")
    public Wrapper<?> login(@Validated @RequestBody LoginVO loginVO, HttpServletRequest request){
        try {
            // 使用AuthenticationManager认证用户
            Authentication authentication = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(loginVO.getUsername(), loginVO.getPassword())
            );
            SecurityContextHolder.getContext().setAuthentication(authentication);
            request.getSession()
                    .setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext());
            return WrapMapper.ok("登录成功!");
        } catch (BadCredentialsException e) {
            return WrapMapper.error("登录失败：" + e.getMessage());
        }
    }

    @GetMapping("/logout")
    public Wrapper<?> logout(){
        return WrapMapper.ok("已成功退出!");
    }



    @GetMapping("/testAsync")
    public Wrapper<?> testAsync(){
        LocalTime start = LocalTime.now();
        /////执行业务逻辑
        userService.test();
        LocalTime end = LocalTime.now();
        return WrapMapper.ok("开始时间 ：" + start  + " \n 结束时间： " + end);
    }

    @GetMapping("/getAllUser")
    public Wrapper getAllUser(){
        List<User> userList = userService.getAllUser();
        return  WrapMapper.ok(userList);
    }

    @GetMapping("/{userId}/getUser")
    public Wrapper getUser(@PathVariable String userId){
        User user = userService.getUserById(userId);
        if( user == null){
            return WrapMapper.error("用户不存在！");
        }else {
            return WrapMapper.ok(user);
        }
    }

    @PostMapping("/register")
    public Wrapper register(@Validated @RequestBody User user){
        return null;
    }


    @PostMapping("/password")
    public Wrapper updatePassword(@Validated @RequestBody LoginVO loginVO){
        return userService.updatePhone(loginVO.getUsername(),loginVO.getPasswordNew());
    }

    @PostMapping("/delete")
    public Wrapper deleteUser(@RequestParam String username){
        return WrapMapper.ok();
    }


}
